/**
 * 获取手机验证码
 */

import { ref } from "vue";

const usePhoneCode = (defaultTime: number = 6) => {
  const time = ref(defaultTime);
  const times = ref(0);
  const wating = ref(false);

  let timer;

  const start = () => {
    times.value++;
    wating.value = true;
    timer = setInterval(() => {
      time.value--;
      if (time.value == 0) {
        finish();
      }
    }, 1000);
  };

  const finish = () => {
    time.value = defaultTime;
    wating.value = false;
    clearInterval(timer);
  };

  return {
    time,
    times,
    wating,
    start,
    finish,
  };
};

export default usePhoneCode;
